#include "../../include/IdManager.h"

IdManager* IdManager::idManager = nullptr;

/**
 * 构造函数
 */
IdManager::IdManager() {}
/**
 * 析构函数
 */
IdManager::~IdManager() {
    clearMap(registerCenter);
}
/**
 * 初始化单例ID构造器
 */
void IdManager::initIdManager() {
    if(IdManager::idManager != nullptr) {
        delete IdManager::idManager;
    }
    IdManager::idManager = new IdManager();
}
/**
 * 获取ID构造器
 */
IdManager* IdManager::getIdManager() {
    return IdManager::idManager;
}
/**
 * 析构单例ID构造器
 */
void IdManager::deteleIdManager() {
    if(IdManager::idManager == nullptr) {
        return;
    }
    delete IdManager::idManager;
}

/**
 * 判断该名称是否登记为ID
 */
bool IdManager::isRegistered(const string &name) {
    return registerCenter.find(name) != registerCenter.end();
}

/**
 * 判断该名称下是否存在该ID
 */
bool IdManager::isIdExist(const string &name,const string &id) {
    if(!isRegistered(name)) {
        return false;
    }
    IdRegister* regi = registerCenter[name];
    return regi->idSet.find(id) != regi->idSet.end();
}

/**
 * 注册ID登记
 */
bool IdManager::registered(const string &name,const string &front,const int length) {
    if(isRegistered(name)) {
        return false;
    }
    IdRegister* newReg = new IdRegister(front,length);
    registerCenter[name] = newReg;
    return true;
}
/**
 * 创建一个ID
 * @param string name 名称
 */
string IdManager::createId(const string &name) {
    IdRegister* regi = registerCenter[name];
    int count = regi->idSet.size();
    string newId = "";
    while(isIdExist(name,newId) || newId == "") {
        count += 1;
        string Id = regi->front;
        string num = to_string(count);
        int zeroCount = regi->length - regi->front.length()-num.length();
        for(int i=0;i<zeroCount;i++) {
            Id += '0';
        }
        Id += num;
        newId = Id;
    }
    regi->idSet.insert(newId);
    return newId;
}
/**
 * 增加一个ID
 * @param string name 名称
 */
bool IdManager::addId(const string &name,const string &id) {
    if(isIdExist(name,id)) {
        return false;
    }
    IdRegister* regi = registerCenter[name];
    regi->idSet.insert(id);
    return true;
}
/**
 * 删除一个ID
 * @param string name 名称
 */
bool IdManager::removeId(const string &name,const string &id) {
    if(!isIdExist(name,id)) {
        return false;
    }
    IdRegister* regi = registerCenter[name];
    regi->idSet.erase(id);
    return true;
}
/**
 * 保存数据
 */
bool IdManager::saveData() {
    ofstream fout(IdRegisterDataPath);
    fout << registerCenter.size() << endl;
    unordered_map<string,IdRegister*>::iterator iter;
    for(iter=registerCenter.begin();iter!=registerCenter.end();iter++) {
        fout << iter->first << " " << iter->second->toString() << endl;
    }
    return true;
}
/**
 * 加载数据
 */
bool IdManager::loadData() {
    ifstream fin(IdRegisterDataPath);
    int regCount;
    string line;

    getline(fin,line);
    if(line == "") {
        return false;
    }
    regCount = stoi(line);
    for(int i=0;i<regCount;i++) {
        getline(fin,line);
        istringstream iss(line);
        string name;
        getline(iss,name,' ');
        IdRegister* idReg = IdRegister::strToIdRegister(iss);
        registerCenter[name] = idReg;
    }
    return true;
}